﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vit.Common.Helpers;

namespace Vit.PlugIn.Lucene.Converters
{
    public class ReflectEntityAccessor : IEntityAccessor
    {
        public ReflectEntityAccessor(Type entityType)
        {
            this.EntityType = entityType;
        }

        #region IEntityAccessor Members

        public void CreateNewEntity()
        {
            ReflectionHelper.CreateInstance(this.EntityType, null);
        }

        public string GetField(string name)
        {
            object obj = ReflectionHelper.GetPropertyValue(this.EntityType, name);
            return obj as string ?? string.Empty;
        }

        public void SetField(string name, string value)
        {
            ReflectionHelper.SetPropertyValue(this.EntityType, name, value);
        }

        public object Entity { get; set; }

        #endregion

        public Type EntityType { get; private set; }
    }
}
